import requests
from lxml import etree
import pymongo
import time
class get_ip:
    def __init__(self):
        _mongo = pymongo.MongoClient(host="127.0.0.1",port=27017)
        db = _mongo.IPS
        self.ip_table = db["ip_table"]

    def get_html(self,page):
        headers = {
            'Connection': 'keep-alive',
            'sec-ch-ua': '"Google Chrome";v="87", " Not;A Brand";v="99", "Chromium";v="87"',
            'sec-ch-ua-mobile': '?0',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Sec-Fetch-Site': 'same-origin',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-User': '?1',
            'Sec-Fetch-Dest': 'document',
            'Referer': f'https://www.kuaidaili.com/free/inha/{page}/',
            'Accept-Language': 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7',
        }

        response = response = requests.get(f'https://www.kuaidaili.com/free/inha/{page}/', headers=headers)
        html = etree.HTML(response.text)
        trs = html.xpath('//div[@id="list"]//table/tbody/tr')
        print(trs)
        print(response)
        for tr in trs:
            ip = tr.xpath('td[@data-title="IP"]/text()')[0]
            port = tr.xpath('td[@data-title="PORT"]/text()')[0]
            print(f"{ip}:{port}")
            _find = self.ip_table.find_one({"ip":ip,"post":port})
            # 去重
            if not _find:
                print("ip不存在")
                self.ip_table.insert_one({"ip":ip,"post":port})
            else:
                print("ip已存在")
        
        
g = get_ip()
for i in range(1,9999):
    g.get_html(i)
    time.sleep(3)